package com.zdkj.subway.check.controller.subwayRat.TVMRevenueVarianceAudit.TvmChaYiDXC;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.check.controller.CommonController;
import com.zdkj.subway.check.entity.Page;
import com.zdkj.subway.check.entity.SwAuTvmBoxAmountDifference;
import com.zdkj.subway.check.entity.SwAuTvmMoneyAssist;
import com.zdkj.subway.check.entity.SwAuTvmSingleAssist;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuTvmBoxAmountDifferenceMapper;
import com.zdkj.subway.check.mapper.SwAuTvmMoneyAssistMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.SwAuTvmMoneyAssistService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 数据协查管理-TVM票款差异协查-TVM票款差异协查
 * 数据协查管理-TVM票款差异协查-TVM系统差异-TVM系统数据差异
 * TVM票款差异协查-票款差异明细表 SW_AU_TVM_MONEY_ASSIST 根据不同权限修改
 */
@Slf4j
@RestController
@RequestMapping("/checkNew")
public class SwAuTvmMoneyAssistCtrl {

    @Resource
    private SwAuTvmMoneyAssistService swAuTvmMoneyAssistService;
    @Resource
    private SwAuTvmMoneyAssistMapper swAuTvmMoneyAssistMapper;
    @Resource
    SwAuTvmBoxAmountDifferenceMapper swAuTvmBoxAmountDifferenceMapper;
    @Autowired
    private CommonController commonController;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 导出
     * @param
     * @return
     */
    @ApiOperation(value = "TVM分钱箱票款导出", notes = "TVM分钱箱票款导出")
    @GetMapping({"/exportTvmMoneyAssist"})
    @SysLog("TVM分钱箱票款导出")
    public void export(String ids, String tag, SwAuTvmMoneyAssist params, HttpServletResponse response) throws ParseException {
        swAuTvmMoneyAssistService.export(ids,tag,params,response);
    }
    /**
     * TVM票款差异协查-票款差异明细表 列表
     * @param param
     * @return
     */
    /*@RequestMapping("get_xw_SwAuTvmMoneyAssistList")
    public Page get_xw_SwAuTvmMoneyAssistList(Page page, @RequestParam Map<String,String> param){
        param.put("xw_assistInvestStatus","1");
        List<Map<String, Object>> tvmTicketCYXCList = swAuTvmMoneyAssistMapper.getTvmTicketCYXCList(param);
        page.setData(tvmTicketCYXCList,true);
        return page;
    }*/

    /**
     * TVM票款差异协查 客运列表
     * @param param
     * @return
     */
    @RequestMapping("/get_xw_SwAuTvmMoneyAssistList")
    @ApiOperation(value = "TVM票款差异协查 客运列表", notes = "TVM票款差异协查 客运列表")
    public Page get_xw_SwAuTvmMoneyAssistList(Page page, @RequestParam Map<String,String> param){
        //swAuTvmMoneyAssist.setAssistInvestStatus("1");
        List<Map<String, Object>> swAuTvmMoneyAssistList = swAuTvmMoneyAssistMapper.getTvmMoneyAssistList(param);
        page.setData(swAuTvmMoneyAssistList,true);
        return page;
        /*JSONObject resultJson = new JSONObject();
        resultJson.put("list", swAuTvmMoneyAssistList);
        PageInfo<SwAuTvmMoneyAssist> swAuTvmMoneyAssistPageInfo = new PageInfo(swAuTvmMoneyAssistList);
        resultJson.put("pageInfo", swAuTvmMoneyAssistPageInfo);
        return new Result(ResultDetailMsg.OK, resultJson);*/
    }

    /**
     * TVM票款差异协查 其他运营中心列表
     * @param swAuTvmMoneyAssist
     * @return
     */
    @RequestMapping("/get_xw_SwAuTvmMoneyAssistListOther")
    @ApiOperation(value = "TVM票款差异协查 其他运营中心列表", notes = "TVM票款差异协查 其他运营中心列表")
    public Result get_xw_SwAuTvmMoneyAssistListOther(@RequestBody SwAuTvmMoneyAssist swAuTvmMoneyAssist){
        //查询协查状态为待下发的数据
        swAuTvmMoneyAssist.setAssistInvestStatus("1");
        List<Map<String, Object>> swAuTvmSingleAssistList = swAuTvmMoneyAssistMapper.getTvmMoneyAssistList(swAuTvmMoneyAssist);
        JSONObject resultJson = new JSONObject();
        resultJson.put("list", swAuTvmSingleAssistList);
        PageInfo<SwAuTvmSingleAssist> swAuTvmSingleAssistPageInfo = new PageInfo(swAuTvmSingleAssistList);
        resultJson.put("pageInfo", swAuTvmSingleAssistPageInfo);
        return new Result(ResultDetailMsg.OK, resultJson);
    }
    /**
     * TVM票款差异协查 机电列表
     * @param param
     * @return
     */
    @RequestMapping("/get_xw_SwAuTvmMoneyAssistListJD")
    @ApiOperation(value = "TVM票款差异协查 机电列表", notes = "TVM票款差异协查 机电列表")
    public Page get_xw_SwAuTvmMoneyAssistListJD(Page page, @RequestParam Map<String,String> param){
        /*swAuTvmMoneyAssist.setAssistInvestStatus("1");
        if(StringUtils.isEmpty(swAuTvmMoneyAssist.getLineCode())) {
            List<String> lineCodeList = new ArrayList<>();
            PigxUser user = SecurityUtils.getUser();
            Map<String, Object> p1 = new HashMap<>();
            p1.put("user_code", user.getUserCode());
            List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
            SysUser sysUser = sysUsers.get(0);
            String managementLineStation = sysUser.getManagementLineStation();
            if (!StringUtils.isEmpty(managementLineStation)) {
                JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
                for (int i = 0; i < managementLineStations.size(); i++) {
                    JSONObject jsonObject1 = managementLineStations.getJSONObject(i);
                    //存在多条线路
                    lineCodeList.add(jsonObject1.getString("line"));
                }
                swAuTvmMoneyAssist.setLineCode(org.apache.commons.lang3.StringUtils.join(lineCodeList,","));
            }else {
                //不管辖线路
                swAuTvmMoneyAssist.setLineCode(sysUser.getLineCode());
            }
        }
        //swAuTvmMoneyAssist.setLineCode(SecurityUtils.getUser().getLineCode());
        List<Map<String, Object>> swAuTvmSingleAssistList = swAuTvmMoneyAssistMapper.getTvmMoneyAssistList(swAuTvmMoneyAssist);
        JSONObject resultJson = new JSONObject();
        resultJson.put("list", swAuTvmSingleAssistList);
        PageInfo<SwAuTvmSingleAssist> swAuTvmSingleAssistPageInfo = new PageInfo(swAuTvmSingleAssistList);
        resultJson.put("pageInfo", swAuTvmSingleAssistPageInfo);
        return new Result(ResultDetailMsg.OK, resultJson);*/
        List<Map<String, Object>> swAuTvmSingleAssistList = swAuTvmMoneyAssistMapper.getTvmMoneyAssistList(param);
        page.setData(swAuTvmSingleAssistList,true);
        return page;
    }

    /**
     * TVM票款差异协查-票款差异明细表 单独查询
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuTvmMoneyAssist")
    @ApiOperation(value = "TVM票款差异协查-票款差异明细表 单独查询", notes = "TVM票款差异协查-票款差异明细表 单独查询")
    public Result getSwAuTvmMoneyAssist(@RequestBody JSONObject jsonObject){
        return swAuTvmMoneyAssistService.getSwAuTvmMoneyAssist(jsonObject);
    }

    /**
     * 获取票款差异分钱箱信息
     * @param swAuTvmBoxAmountDifference
     * @param ids
     * @return
     */
    @RequestMapping("/get_xw_SwAuTvmBoxAmountDifference")
    @ApiOperation(value = "获取票款差异分钱箱信息", notes = "获取票款差异分钱箱信息")
    public Result get_xw_SwAuTvmBoxAmountDifference(SwAuTvmBoxAmountDifference swAuTvmBoxAmountDifference, String ids){
        Result result=new Result(ResultDetailMsg.OK);
        try{
            List<Map<String,Object>> res=swAuTvmBoxAmountDifferenceMapper.getSwAuTvmBoxAmountDifference(swAuTvmBoxAmountDifference);
            result.setData(res);
            return result;
        }catch (Exception e){
            log.error("{}=>{}","查询数据失败！",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.setMsg("查询数据失败");
            return result;
        }
    }

    /**
     * TVM票款差异协查-票款差异明细表 修改
     * @param jsonObject
     * @return
     */
    @RequestMapping("/updateSwAuTvmMoneyAssistToStatus")
    @ApiOperation(value = "TVM票款差异协查-票款差异明细表 修改", notes = "TVM票款差异协查-票款差异明细表 修改")
    @Transactional(rollbackFor = Exception.class)
    public Result updateSwAuTvmMoneyAssistToStatus(@RequestBody JSONObject jsonObject){
        try{
            if(null != jsonObject.getJSONArray("ids")){
                JSONArray ids = jsonObject.getJSONArray("ids");
                for(Object idObj: ids){
                    Integer id = Integer.valueOf(idObj.toString());
                    SwAuTvmMoneyAssist swAuTvmMoneyAssist = new SwAuTvmMoneyAssist();
                    swAuTvmMoneyAssist.setId(id);
                    swAuTvmMoneyAssist.setKyReplyStatus("1");
                    swAuTvmMoneyAssist.setKyAssistInvestStatus("3");
                    swAuTvmMoneyAssistMapper.updateById(swAuTvmMoneyAssist);
                }
                Map param = JSONObject.parseObject(jsonObject.toString(),Map.class);
                //下发代办
                commonController.innerBatch(param);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return new Result(ResultDetailMsg.OK);
    }
    /**
     * TVM票款差异协查-票款差异明细表 修改
     * @param jsonObject
     * @return
     */
    @RequestMapping("/updateSwAuTvmMoneyAssist")
    @ApiOperation(value = "TVM票款差异协查-票款差异明细表 修改", notes = "TVM票款差异协查-票款差异明细表 修改")
    public Result updateSwAuTvmMoneyAssist(@RequestBody JSONObject jsonObject){
        if(null != jsonObject.getJSONArray("ids")){
            JSONArray ids = jsonObject.getJSONArray("ids");
            for(Object idObj: ids){
                Integer id = Integer.valueOf(idObj.toString());
                //TVM票款差异协查-票款差异明细表 修改
                this.updateSwAuTvmMoneyAssistById(jsonObject,id);
            }
        }
        return new Result(ResultDetailMsg.OK);
    }

    /**
     * TVM票款差异协查-票款差异明细表 修改
     * @param jsonObject
     * @param id
     */
    private void updateSwAuTvmMoneyAssistById(JSONObject jsonObject,Integer id){
        String kyReplyReason = jsonObject.getString("kyReplyReason");
        String kyReplyDetail = jsonObject.getString("kyReplyDetail");
        SwAuTvmMoneyAssist swAuTvmMoneyAssist = new SwAuTvmMoneyAssist();
        swAuTvmMoneyAssist.setId(id);
        //回馈原因
        swAuTvmMoneyAssist.setKyReplyReason(kyReplyReason);
        //回馈描述
        swAuTvmMoneyAssist.setKyReplayConent(kyReplyDetail);
        swAuTvmMoneyAssistMapper.updateById(swAuTvmMoneyAssist);
    }

    /**
     * 提交功能，修改回馈状态
     * @param param
     * @return
     */
    @RequestMapping("/submitData_xw_SwAuTvmMoneyAssist")
    @ApiOperation(value = "提交功能，修改回馈状态", notes = "提交功能，修改回馈状态")
    @Transactional(rollbackFor = Exception.class)
    public Result submitData_xw_SwAuTvmMoneyAssist(@RequestBody Map<String,Object> param){
        String ids = String.valueOf(param.get("ids"));
        Result result=new Result(ResultDetailMsg.OK);
        try {
            for(String id:ids.split(",")){

                SwAuTvmMoneyAssist swAuTvmMoneyAssist=new SwAuTvmMoneyAssist();
                swAuTvmMoneyAssist.setId(Integer.parseInt(id));
                swAuTvmMoneyAssist.setJdReplyStatus("1");
                swAuTvmMoneyAssist.setAssistInvestStatus("3");
                swAuTvmMoneyAssist.setReplyTime(new Date());
//                swAuTvmMoneyAssist.setOperatorId(UserInfo.getId());
//                swAuTvmMoneyAssist.setOperatorCode(UserInfo.getUserCode());
                int i = swAuTvmMoneyAssistMapper.updateById(swAuTvmMoneyAssist);
            }
            //commonController.innerBatch(param);//tvm票款差异 客运的代办改为下发后直接发代办
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("提交出错！");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }


    /**
     * 线网的-数据协查-票款差异-编辑数据
     * @param jsonObject
     * @return
     */
    @RequestMapping("/editor_xw_SwAuTvmBoxAmountDifference")
    @ApiOperation(value = "线网的-数据协查-票款差异-编辑数据", notes = "线网的-数据协查-票款差异-编辑数据")
    public Result editor_xw_SwAuTvmBoxAmountDifference(@RequestBody JSONObject jsonObject){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            SwAuTvmMoneyAssist swAuTvmMoneyAssist=JSONObject.parseObject(jsonObject.toJSONString(),SwAuTvmMoneyAssist.class);
            //swAuTvmMoneyAssist.setOperatorCode(UserInfo.getUserCode());
            //swAuTvmMoneyAssist.setOperatorId(UserInfo.getId());
            int i = swAuTvmMoneyAssistMapper.updateById(swAuTvmMoneyAssist);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("编辑出错！");
        }
        return result;
    }

    /**
     * 撤回TVM分钱箱票款差异 撤回
     * @param
     * @return
     */
    @RequestMapping("/revokeTvmFenQianXiang")
    @ApiOperation(value = "撤回TVM分钱箱票款差异 撤回", notes = "撤回TVM分钱箱票款差异 撤回")
    public R revokeTvmFenQianXiang(String ids) {
        List<SwAuTvmMoneyAssist> tvmMoneyAssists = swAuTvmMoneyAssistMapper.selectBatchIds(Arrays.asList(ids.split(",")));
        if(CollectionUtil.isNotEmpty(tvmMoneyAssists)){
            for(SwAuTvmMoneyAssist tvmMoneyAssist:tvmMoneyAssists){
                if("1".equals(tvmMoneyAssist.getReplaceStatus())){
                    return R.failed("已经替换的数据不能撤回！");
                }
                SwAuTvmMoneyAssist s=new SwAuTvmMoneyAssist();
                s.setId(tvmMoneyAssist.getId());
                s.setJdReplyStatus("0");//机电回馈状态改为0，未回馈
                s.setAssistInvestStatus("2");//协查状态改为2，待协查
                swAuTvmMoneyAssistMapper.updateById(s);
            }
        }
        return R.ok();
    }
}
